package com.martinwj.controller.history;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.martinwj.constant.ErrorMsg;
import com.martinwj.entity.History;
import com.martinwj.entity.Result;
import com.martinwj.entity.User;
import com.martinwj.exception.SysException;
import com.martinwj.service.HistoryService;
import com.martinwj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: HistoryAction
 * @Description: TODO
 * @author: martin-wj
 * @createDate: 2021-04-05
 */
@Controller
@RequestMapping("portal/history_info")
public class HistoryAction {

    @Autowired
    private HistoryService historyService;
    @Autowired
    private UserService userService;

    /**
     * 保存用户的播放记录
     * @throws SysException
     */
    @RequestMapping("save.json")
    @ResponseBody
    public Result save(
            HttpServletRequest request,
            @RequestParam(value="videoId") String videoId,
            @RequestParam(value="userToken") String userToken) throws SysException {

        if (StringUtils.isEmpty(userToken)) {
            throw new SysException(ErrorMsg.ERROR_100012);
        }

        // 判断session
        HttpSession session  = request.getSession();
        // 从session中取出用户身份信息
        User userInfo = (User)session.getAttribute("userInfo");

        if (userInfo==null) {
            userInfo = userService.getUserInfoByUserToken(userToken);
            // 将用户信息保存进session
            request.getSession().setAttribute("userInfo", userInfo);
        }

        History historyInfo = new History();
        historyInfo.setVideoId(videoId);
        historyInfo.setUserId(userInfo.getId());

        historyService.save(historyInfo);

        return Result.success();
    }

    /**
     * 获取最新的5条历史记录
     * @param request
     * @param userToken
     * @return
     * @throws SysException
     */
    @RequestMapping("get_history_5.json")
    @ResponseBody
    public Result getHistory5(
            HttpServletRequest request,
            @RequestParam(value="userToken") String userToken) throws SysException {

        if (StringUtils.isEmpty(userToken)) {
            throw new SysException(ErrorMsg.ERROR_100012);
        }

        // 判断session
        HttpSession session  = request.getSession();
        // 从session中取出用户身份信息
        User userInfo = (User)session.getAttribute("userInfo");

        if (userInfo==null) {
            userInfo = userService.getUserInfoByUserToken(userToken);
            // 将用户信息保存进session
            request.getSession().setAttribute("userInfo", userInfo);
        }

        List<Map<String, Object>> list = historyService.listHistory5(userInfo.getId());

        return Result.success().add("list", list);
    }

    /**
     * 获取历史记录列表
     * @param request
     * @param userToken 用户凭证
     * @param pageNum
     * @param pageSize
     * @return
     * @throws SysException
     */
    @RequestMapping("get_history_list.json")
    @ResponseBody
    public Result getHistoryList(
            HttpServletRequest request,
            @RequestParam(value="userToken") String userToken,
            @RequestParam(value="pageNum", defaultValue="1") int pageNum,
            @RequestParam(value="pageSize", defaultValue="15") int pageSize) throws SysException {

        if (StringUtils.isEmpty(userToken)) {
            throw new SysException(ErrorMsg.ERROR_100012);
        }

        // 判断session
        HttpSession session  = request.getSession();
        // 从session中取出用户身份信息
        User userInfo = (User)session.getAttribute("userInfo");

        if (userInfo==null) {
            userInfo = userService.getUserInfoByUserToken(userToken);
            // 将用户信息保存进session
            request.getSession().setAttribute("userInfo", userInfo);
        }

        // pageHelper分页插件
        // 只需要在查询之前调用，传入当前页码，以及每一页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = historyService.listHistory(userInfo.getId());
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(list);

        return Result.success().add("pageInfo", pageInfo);
    }

    /**
     * 删除历史记录
     * @param request
     * @param videoId 视频主键
     * @param userToken 用户凭证
     * @return
     * @throws SysException
     */
    @RequestMapping("delete_history.json")
    @ResponseBody
    public Result deleteHistory(
            HttpServletRequest request,
            @RequestParam(required=false, value="videoId") String videoId,
            @RequestParam(value="userToken") String userToken) throws SysException {

        if (StringUtils.isEmpty(userToken)) {
            throw new SysException(ErrorMsg.ERROR_100012);
        }

        // 判断session
        HttpSession session  = request.getSession();
        // 从session中取出用户身份信息
        User userInfo = (User)session.getAttribute("userInfo");

        if (userInfo==null) {
            userInfo = userService.getUserInfoByUserToken(userToken);
            // 将用户信息保存进session
            request.getSession().setAttribute("userInfo", userInfo);
        }

        History history = new History();
        history.setVideoId(videoId);
        history.setUserId(userInfo.getId());

        historyService.delete(history);

        return Result.success();
    }
}
